<template>
  <div>
    <el-tree
      :data="data"
      show-checkbox
      node-key="id"
      default-expand-all
      :props="defaultProps"
      :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ data.label }}</span>
        <span class="button">
          <el-button
            type="text"
            size="mini"
            @click="() => append(data)">
            编辑
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => remove(node, data)">
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
  </div>
</template>

<script>
let id = 1000;
export default {
    data() {
      return {
        dialogFormVisible: false,
        data: [{
          id: 1,
          label: '新品',
          children: [{
            id: 4,
            label: 'CGLDG系列'
          },{
            id: 5,
            label:'BZDZLLY系列',
          }]
        }, {
          id: 2,
          label: '高端定制',
          children: [{
            id: 6,
            label: '轻奢家居'
          }, {
            id: 7,
            label: '意式极简'
          }]
        }, {
          id: 3,
          label: '展厅现货',
          children: [{
            id: 8,
            label: '现代极简'
          }]
        }],
        defaultProps: {
          children: 'children', //指定子树为节点对象的某个属性值--选择一个对象作为子节点使用
          label: 'label' //指定节点标签为节点对象的某个属性值--选择一个值作为label显示
        }
      };
    },
    methods:{
    append(data) {
        const newChild = { id: id++, label: 'testtest', children: [] };
        if (!data.children) {
          this.$set(data, 'children', []);
        }
        data.children.push(newChild);
      },
      remove(node, data) {
        const parent = node.parent;
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
      },
    }
  };
</script>

<style scoped>
.button{
  position: absolute;
  margin-left: 10%;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>